package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import com.itheima.dao.CheckItemDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
//        System.out.println(queryPageBean.getQueryString().length());
        //基于mybatis框架提供的分页插件助手完成
        if (queryPageBean.getQueryString()!=null&&queryPageBean.getQueryString().length()!=0){
            PageHelper.startPage(1,10);
        }else{
            PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        }
       // PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckItem> page=checkItemDao.selectByCondition(queryPageBean.getQueryString());
        //分页插件获取总数total 和list集合rows=page.getResult()来封装成PageResult对象并返回
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void delete(Integer id) {
        //检查是否有关联
        long count =checkItemDao.findCountByCheckItemId(id);
        if (count>0){
            //当前检查项被引用，不能删除
            throw new RuntimeException("当前检查项被引用，不能删除");
        }
        checkItemDao.deleteById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    @Override
    public List<CheckItem> findAll() {
       List<CheckItem> list= checkItemDao.selectByCondition("");
       return list;
    }
}
